import { Component, OnInit, Output } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { endOfMonth, format } from 'date-fns';
import { zhCN } from 'date-fns/locale'; // 设置locale
@Component({
  selector: 'app-last-year',
  templateUrl: './last-year.component.html',
  styleUrls: ['./last-year.component.css'],
})
export class LastYearComponent implements OnInit {
  public year: number = 6;
  constructor(private route: ActivatedRoute) {}
  public lastYearList: any = [];
  ngOnInit() {
    this.route.queryParams.subscribe((param) => {
      this.year = param['year'] > 20 ? 20 : param['year'] < 0 ? 1 : 6;
    });
    // 获取当前年份
    const startYear = new Date().getFullYear();
    let yearList: any = [];

    let i = 0;
    while (i < this.year) {
      yearList.push(startYear - i);
      i++;
    }
    yearList.forEach((i: number) => {
      // startOfMonth(date: number | Date): Date
      let endDay = endOfMonth(new Date(i + '-02')); //Thu Jan 31 2019 23:59:59 GMT+0800 (中国标准时间)
      this.lastYearList.push(format(endDay, 'yyyy-MM-dd ')); // 2019-01-31 23:59:59
    });
  }
}
